import Vue from 'vue'
import Router from 'vue-router'
import { isEmpty } from '../utils/util'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(Router)

const defaultRoutes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login')
  },
  {
    path: '/index',
    name: 'index',
    component: () => import('../views/index'),
    redirect: '/stat',
    children: [],

    // children: [{ path: '/stat', component: () => import('../views/stat')}]
  }
]

const dynamicRoutes = [
    {
        path: '/user',
        name: 'user',
        component: () => import('../views/system/user')
    },
    {
        path: '/area',
        name: 'area',
        component: () => import('../views/system/area')
    },
    {
        path: '/guard',
        name: 'guard',
        component: () => import('../views/system/guard')
    },
    {
        path: '/param',
        name: 'param',
        component: () => import('../views/system/param')
    },
    {
        path: '/role',
        name: 'role',
        component: () => import('../views/system/role')
    },
    {
        path: '/department',
        name: 'department',
        component: () => import('../views/system/department')
    },
    {
        path: '/log',
        name: 'log',
        component: () => import('../views/system/log')
    },
  //  createFile
  {
      path: '/createFile',
      name: 'createFile',
      component: () => import('../views/fileManagement/createFile')
  },
  {
      path: '/fileRecord',
      name: 'fileRecord',
      component: () => import('../views/fileManagement/fileRecord'),
  },
  {
      path: '/jzRecord',
      name: 'jzRecord',
      component: () => import('../views/fileManagement/jzRecord')
  },
  {
      path: '/outRecord',
      name: 'outRecord',
      component: () => import('../views/fileManagement/outRecord')
  },
  {
      path: '/backRecord',
      name: 'backRecord',
      component: () => import('../views/fileManagement/backRecord')
  },
  {
    path: '/recordsType',
    name: 'recordsType',
    component: () => import('../views/setting/recordsType'),
  },
  {
    path: '/recordsLevel',
    name: 'recordsLevel',
    component: () => import('../views/setting/recordsLevel')
  },
  {
        path: '/jzType',
        name: 'jzType',
        component: () => import('../views/setting/jzType')
  },
  {
      path: '/jzStore',
      name: 'jzStore',
      component: () => import('../views/setting/jzStore')
  },
  {
    path: '/stat',
    name: 'stat',
    component: () => import('../views/stat')
  },
//   {
//   path: '/stat',
//   name: 'stat',
//   component: () => import('../views/visulaShow/stat')
// }
]

const router = new Router({
  routes: [...defaultRoutes]
})

//解决 Vue 重复点击相同路由，出现 Uncaught
const VueRouterPush = Router.prototype.push
Router.prototype.push = function push (to) {
    return VueRouterPush.call(this, to).catch(err => err)
}


NProgress.configure({ showSpinner: false, easing: 'ease', speed: 2000 })

router.beforeEach((to, from, next) => {
  if (to.name !== 'login') {
    let _token = sessionStorage.getItem('token')
    if (isEmpty(_token)) {
      next({
        name: 'login',
        query: { redirect: to.fullPath }
      })
    } else {
      NProgress.start()
      if (router.getRoutes().length === 3) {
        const routerList = sessionStorage.getItem('routerList')
        addDynamicRoutes(JSON.parse(routerList))
        next({ ...to, replace: true })
      } else {
        next()
      }
    }
  } else {
    NProgress.start()
    next()
  }
})
router.afterEach(() => {
  NProgress.done()
})

export function addDynamicRoutes(routerList) {
  dynamicRoutes.forEach(v => {
    if (routerList.findIndex(vv => vv.component === v.name) !== -1) {
      router.addRoute('index', v)
    }
  })
}

export default router
